<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Guarantee extends CI_Controller {

    private $tab = 'borrow';

    function __construct() {
        parent::__construct();
        /** 登陆验证 */
        if (!isset($_SESSION)) {
            session_start();
        }

        $admin = $this->session->userdata('admin_user');
        if (!$admin || !isset($admin['user_id'])) {
            header('Location: /ychadmin/login');
            exit;
        }
    }

    function add_view() {

        $this->load->model('Attestation_type_model', 'attestation_type');
        $data['type_list'] = $this->attestation_type->get_id_names();

        $this->load->model('borrow_model');
        $data['borrow_guarantee'] = $this->borrow_model->arrays('borrow_guarantee');

        $data['sub'] = 'guarantee_add';
        $data['tab'] = $this->tab;

        $this->load->view("/ychadmin/guarantee_add", $data);
    }

    function add() {
        $data['type'] = intval($this->input->post('type'));
        $data['name'] = $this->input->post('name');
        $data['account'] = $this->input->post('account');
        $data['province'] = $this->input->post('province');
        $data['city'] = $this->input->post('city');
        $data['address'] = $this->input->post('address');
        $data['contact'] = $this->input->post('contact');
        $data['web'] = $this->input->post('web');
        $data['establishment_time'] = strtotime($this->input->post('establishment_time'));
        $data['collaboration_time'] = strtotime($this->input->post('collaboration_time'));
        $data['introduction'] = $this->input->post('introduction');

        $isActive = $this->input->post('isActive');
        if (!$isActive){
            $data['isActive'] = 0;
        } else {
            $data['isActive'] = 1;
        }
        if ($data['type'] == 3) {
            $data['letter_guarantee'] = $this->input->post('letter_guarantee');
        } else {
            $data['risk_measures'] = $this->input->post('risk_measures');
            $data['collateral'] = $this->input->post('collateral');
        }

        $this->load->model('borrow_model');
        $id = intval($this->input->post('id'));
        //检查其他人有没有用过这个别名 别名不能重复
        $alias = $this->input->post('alias');
        $grtList = $this->borrow_model->get_guarantee_list(array(0 => ' is_del = 0 and alias = "' . $alias . '" and id != ' . $id));
        if ($grtList['count'] > 0) {
            echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />';
            echo "<script>alert('别名为空或别名已经存在!');history.go(-1);</script>";
            exit;
        }
        $data['alias'] = $alias;


        $act = $this->input->post('act');
        if ($act == 'add') {
            $guarantee_id = $this->borrow_model->guarantee_add($data);
        } else {
            $res = $this->borrow_model->guarantee_update($id, $data);
            $guarantee_id = $id;
        }


        if ($guarantee_id > 0) {
            $this->load->model('Attestation_model', 'attestation');

            $att_id = $this->input->post('att_id');
            if (isset($att_id) && $att_id) {

                $att_ret = $this->attestation->get_list_guarantee_by_id($att_id, $guarantee_id);
                if ($att_ret > 0) {
                    $this->load->model('log_model');
                    $this->log_model->add_log('[添加][担保机构]');
                }
            }
            header("location:/ychadmin/guarantee/guaranteelist");
            exit;
        }
    }

    function edit_view($guarantee_id) {
        $this->load->model('borrow_model');
        $guarantee = $this->borrow_model->get_borrow_guarantee_obj($guarantee_id);
        $this->load->model('Attestation_model', 'attestation');
        $vdata['att_list'] = $this->attestation->get_list_guarantee($guarantee_id);

        $this->load->model('Attestation_type_model', 'attestation_type');
        $vdata['type_list'] = $this->attestation_type->get_id_names();

        $vdata['borrow_guarantee'] = $this->borrow_model->arrays('borrow_guarantee');


        $vdata['guarantee'] = $guarantee;
        $vdata['sub'] = 'guarantee_list';
        $vdata['tab'] = $this->tab;
        $this->load->view("/ychadmin/guarantee_edit", $vdata);
    }

    function guaranteelist() {
        $this->load->model('borrow_model');

        $per_page = intval($this->input->get('per_page'));
        $page_size = 10;

        $parms = array(
            'name' => trim($this->input->get('name')),
            'is_del' => 0,
        );

        $vdata = $this->borrow_model->get_guarantee_list($parms, $per_page, $page_size);

        //分页
        $this->load->library('pagination');
        $config['base_url'] = '/ychadmin/guarantee/guaranteelist/?name=' . $parms['name'];
        $config['total_rows'] = $vdata['count'];
        $config['per_page'] = 10;
        $this->pagination->initialize($config);

        $this->load->model('Area_model', 'area');
        $vdata['area_list'] = $this->area->get_list();

        $vdata['borrow_guarantee'] = $this->borrow_model->arrays('borrow_guarantee');

        $vdata['name'] = $name;
        $vdata['sub'] = 'guarantee_list';
        $vdata['tab'] = $this->tab;
        $this->load->view('/ychadmin/guarantee_list', $vdata);
    }

    function remove() {
        $id = $this->input->post('id');
        $data["is_del"] = 1;
        $this->load->model('borrow_model');
        $this->load->model('log_model');
        $this->log_model->add_log('[删除][担保机构]');
        echo $this->borrow_model->guarantee_update($id, $data);
    }

    function doupload() {
        $ret = array(
            'msg' => '',
            'url' => ''
        );

        $this->load->model('Attestation_model', 'attestation');

        $jifen = 0;
        $time = time();
        $ip = $this->input->ip_address();

        $this->load->model('Attestation_type_model', 'attestation_type');
        $where = array('type_id' => $this->input->post('type_id'));
        $att_info = $this->attestation_type->get_one($where);

        $setArr = array(
            'user_id' => 0,
            'type_id' => $this->input->post('type_id'),
            'name' => $att_info['name'],
            'fstatus' => 1,
            'jifen' => $jifen,
            'content' => $att_info['name'],
            'verify_time' => $time,
            'verify_user' => 1,
            'verify_remark' => $this->input->post('verify_remark'),
            'add_time' => $time,
            'addip' => $ip,
        );

        $path = 'static/data/upfiles/images/guarantee/';
        if (!file_exists($path)) {
            mkdir($path, 0777);
        }
        $config['upload_path'] = $path;
        $config['allowed_types'] = 'jpg|png';
        $config['file_name'] = 'attestation_' . uniqid();
        $config['overwrite'] = true;
        $config['max_size'] = '512';
        $config['max_width'] = '2048';
        $config['max_height'] = '3000';

        $this->load->library('upload', $config);
        if ($this->upload->do_upload('litpic')) {
            $tmp = $this->upload->data();
            $setArr['litpic'] = 'static/data/upfiles/images/guarantee/' . $config['file_name'] . $tmp['file_ext'];

            //添加水印
            $this->load->library('image_lib');
            $warter['source_image'] = $setArr['litpic'];
            $warter['wm_type'] = 'overlay';
            $warter['wm_overlay_path'] = WATERMARK_IMG;
            $warter['wm_opacity'] = '60';
            $warter['wm_vrt_alignment'] = 'middle';
            $warter['wm_hor_alignment'] = 'center';
            $this->image_lib->initialize($warter);
            $this->image_lib->watermark();

            $res = $this->attestation->add($setArr);
            if ($res) {
                $ret['msg'] = array('id' => $res, 'name' => $setArr['name'], 'litpic' => $setArr['litpic']);
            } else {
                $ret['msg'] = '保存失败';
            }
        } else {
            $ret['url'] = 'error';
            $ret['msg'] = $this->upload->display_errors();
        }
        echo json_encode($ret);
        exit;
    }

    function del_att($id = '') {
        $id = intval($id);
        $this->load->model('Attestation_model', 'attestation');
        $att_info = $this->attestation->get_one(array('id' => $id));
        $res = $this->attestation->delete(array('id' => $id, 'guarantee_id' => $att_info['guarantee_id']));
        if ($res) {
            $ret = array('msg' => '删除成功', 'url' => '');
            echo json_encode($ret);
            exit;
        } else {
            $ret = array('msg' => '删除失败', 'url' => '');
            echo json_encode($ret);
            exit;
        }
    }

}
